import java.util.ArrayList;
import java.util.LinkedList;

public class PhraseEntry extends PhraseObject {

    /**
     * A count of how many occurences of this phrase have been found
     */
    int count;
    
    
    
    /**
     * Constructs a phrase entry object with count 0
     *
     */
    public PhraseEntry() 
    {
        this.wordList = new ArrayList();
        this.count = 0;
        this.lineNumbers= new LinkedList();
    }
    

    /**
     * Adds a word to the end of the phrase
     * @param word The word to add
     */
    public void addWord(String word) {
        wordList.add(word);
    }
    
    /**
     * Increments the count by one
     *
     */
    public void increment() {
        /* Test for overflow and report a warning */
        if (count == Integer.MAX_VALUE) {
            System.err.println("Warning, phrase " + super.toString() + " has" +
                    " reached the maximum count supported by signed 32-bit" +
                    "numbers.");
            return;
        }
        count++;
    }
    
    /**
     * Checks if a PhraseObject is equal to this phrase. if so, 
     * increment the count;
     * @param phrase The phrase to compare
     * @return true if equal
     */
    public boolean checkIfEqualAndCount(PhraseObject phrase) {
        boolean ret = this.isPhraseEqual(phrase);
        if (ret) {
            this.increment();
        }
        return ret;
    }

    /**
     * @return Returns the count of phrases.
     */
    public int getCount() {
        return count;
    }
    
    /**
     * 
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return this.getCount() + ": " + super.toString();
    }
    
    /**
     * 
     * @param o The object to compare us to
     * @return negative, 0, or positive if we are less than, equal to
     *  or greater than the other object.
     */
    public int compareTo (Object o) {
        /* First compare based on count */
        if (o instanceof PhraseEntry) {
            int oCount = ((PhraseEntry) o).getCount();
            if (this.count < oCount) {
                return 1;
            }
            if (this.count > oCount) {
                return -1;
            }
        }
        /* If count is equal, compare based on name */
        return super.compareTo(o);
    }

}
